package com.lyrcsoft.webflux_01.function;

import java.util.function.*;

public class FunctionDemo {
    public static void main(String[] args) {

        //================================== 一元函数==================================
        // Consumer 输入T 没有输出
        Product product = new Product();
        // lambda 表达式
        Consumer<Product> consumer = p -> Product.nameOf(p);
        consumer.accept(product);
        // 方法引用
        Consumer<Product> consumer2 = Product::nameOf;
        consumer.accept(product);
        // Supplier 输出T 没有输入
        Supplier<Product> supplier = () -> new Product();
        System.out.println("创建新的构造函数（Supplier）" + supplier.get());
        // Function 一个输入 一个输出
        Function<Integer, Integer> function = product::reduceStock;
        Integer stock = function.apply(10);
        System.out.print("减库存成功，剩余库存（Function）" + stock);
        // 输入输出都是T
        UnaryOperator<Integer> unaryOperator = product::reduceStock;
        System.out.print("减库存成功，剩余库存（UnaryOperator）" + unaryOperator.apply(1));
        // ==================================二元函数==================================
        // 两个输入参数 一个输出
        BiFunction<Integer, Integer, Integer> biFunction = product::reduceStock;
        System.out.print("减库存成功，剩余库存（BiFunction）" + biFunction.apply(100, 5));
        // 输入输出都是T
        BinaryOperator<Integer> binaryOperator = product::reduceStock;
        System.out.print("减库存成功，剩余库存（BinaryOperator）" + binaryOperator.apply(100, 5));
    }
}

class Product {
    private String productName = "手机";
    private Integer stock = 100;

    public static void nameOf(Product product) {
        System.out.println("产品名称：" + product);
    }

    public Integer reduceStock(Integer productNum) {
        System.out.println(" 扣减库存：-" + productNum);
        return stock -= productNum;
    }

    public Integer reduceStock(Integer stock, Integer productNum) {
        System.out.println(" 扣减库存：-" + productNum);
        return this.stock = stock - productNum;
    }

    public Product() {
        // 打印构造函数英文单词
        System.out.println("Product");
    }


    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public Integer getStock() {
        return stock;
    }

    public void setStock(Integer stock) {
        this.stock = stock;
    }

    @Override
    public String toString() {
        return
                "产品名称：'" + productName + '\'' +
                        "" + stock + "台";
    }
}
